package com.ningxun.pagentcrawler.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ningxun.pagentcrawler.entity.policy.PolicyItem;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;
import java.util.Set;

/**
 * 页面解析工具类
 * 专门负责解析网页内容
 */
public class PageParser {

    /**
     * 获取总页数
     */
    public static int getTotalPages(String userAgent) {
        // 配置SSL证书验证忽略
        CrawlerUtil.configureSSL();
        
        int totalPages = 1;
        try {
            String firstPageUrl = UrlBuilder.buildAjaxPageUrl(1);
            Document firstDoc = Jsoup.connect(firstPageUrl)
                    .userAgent(userAgent)
                    .timeout(15000)
                    .get();
            
            Element pageInfo = firstDoc.selectFirst("input[id^=pagingProp_][value*=totalPages]");
            if (pageInfo != null) {
                String json = pageInfo.attr("value").replace("&quot;", "\"");
                
                try {
                    ObjectMapper mapper = new ObjectMapper();
                    JsonNode node = mapper.readTree(json);
                    if (node.has("totalPages")) {
                        totalPages = node.get("totalPages").asInt();
                        System.out.println("✅ 成功解析总页数: " + totalPages);
                    } else {
                        System.err.println("⚠️ JSON中没有找到totalPages字段");
                    }
                } catch (Exception e) {
                    System.err.println("❌ 解析总页数JSON失败: " + e.getMessage());
                    System.err.println("原始JSON: " + json);
                }
            } else {
                System.err.println("⚠️ 未找到分页信息元素");
            }
        } catch (Exception e) {
            System.err.println("获取总页数失败: " + e.getMessage());
        }
        return totalPages;
    }

    /**
     * 解析Ajax分页页面的政策列表
     */
    public static int parsePolicyListFromAjaxPage(Document doc, int pageNum, List<PolicyItem> crawledPolicies, Set<String> urlSet) {
        int pageCount = 0;
        
        // 只选择表格中的政策行，不爬取其他链接
        Elements items = doc.select("table tbody tr");
        
        for (Element item : items) {
            // 只选择表格中标题列的链接
            Element link = item.selectFirst("td:nth-child(2) a");
            if (link != null) {
                String url = link.absUrl("href");
                
                // 过滤无效链接
                if (isValidUrl(url)) {
                    // 进一步过滤：只保留政策详情页链接，过滤导航链接
                    if (isPolicyDetailUrl(url)) {
                        if (!urlSet.contains(url)) {
                            urlSet.add(url);
                            
                            // 从表格中直接获取标题
                            String title = link.text().trim();
                            System.out.println("✅ 找到标题: " + title);
                            
                            // 从表格中直接获取发文字号
                            String documentNumber = "";
                            
                            // 调试：打印当前行的HTML结构
                            System.out.println("🔍 当前行HTML结构: " + item.html());
                            
                            // 调试：检查当前行中的所有p元素
                            Elements pElements = item.select("p");
                            System.out.println("🔍 当前行中找到 " + pElements.size() + " 个p元素");
                            for (int i = 0; i < pElements.size(); i++) {
                                Element p = pElements.get(i);
                                System.out.println("🔍 p元素 " + (i+1) + ": class='" + p.className() + "', text='" + p.text().trim() + "'");
                            }
                            
                            // 尝试多种方式获取文号
                            Element zihaoElement = item.selectFirst(".xxgk_gfxwjk-list-zihao");
                            if (zihaoElement != null) {
                                documentNumber = zihaoElement.text().trim();
                                System.out.println("✅ 找到文号元素: " + documentNumber);
                            } else {
                                System.out.println("❌ 未找到.xxgk_gfxwjk-list-zihao元素");
                                
                                // 尝试p标签选择器
                                Element zihaoElement2 = item.selectFirst("p.xxgk_gfxwjk-list-zihao");
                                if (zihaoElement2 != null) {
                                    documentNumber = zihaoElement2.text().trim();
                                    System.out.println("✅ 找到p.xxgk_gfxwjk-list-zihao元素: " + documentNumber);
                                } else {
                                    System.out.println("❌ 未找到p.xxgk_gfxwjk-list-zihao元素");
                                    
                                    // 尝试从表格第三列获取
                                    Element docNumberCell = item.selectFirst("td:nth-child(3)");
                                    if (docNumberCell != null) {
                                        documentNumber = docNumberCell.text().trim();
                                        System.out.println("✅ 从表格第三列获取文号: " + documentNumber);
                                    } else {
                                        System.out.println("❌ 未找到表格第三列");
                                    }
                                }
                            }
                            
                            // 爬取详情页内容
                            String[] detail = crawlPolicyDetail(url);
                            
                            // 组合标题和发文字号
                            String fullTitle = title;
                            if (!documentNumber.isEmpty()) {
                                fullTitle = title + " " + documentNumber;  // 统一使用空格分隔格式
                            }
                            
                            PolicyItem policy = new PolicyItem();
                            policy.setTitle(fullTitle); // 使用组合后的完整标题
                            policy.setUrl(url);
                            policy.setContent(detail[1]);
                            policy.setDocumentNumber(documentNumber);
                            policy.setIssueDate(""); // 不再获取日期，设置为空字符串
                            crawledPolicies.add(policy);
                            pageCount++;
                            
                            System.out.println("📄 爬取政策详情页: " + fullTitle + " -> " + url);
                            System.out.println("✅ 找到标题: " + title);
                            System.out.println("✅ 找到链接: " + url);
                            if (!documentNumber.isEmpty()) {
                                System.out.println("✅ 找到文号: " + documentNumber);
                            } else {
                                System.out.println("❌ 未找到文号元素");
                            }
                        } else {
                            System.out.println("⚠️ 跳过重复政策: " + url);
                        }
                    } else {
                        System.out.println("⚠️ 跳过导航链接: " + url);
                    }
                } else {
                    System.out.println("⚠️ 跳过无效链接: " + url);
                }
            }
        }
        return pageCount;
    }
    
    /**
     * 验证URL是否有效
     */
    private static boolean isValidUrl(String url) {
        if (url == null || url.isEmpty()) {
            return false;
        }
        
        // 过滤javascript:void(0)
        if (url.equals("javascript:void(0)") || url.startsWith("javascript:")) {
            return false;
        }
        
        // 过滤空链接
        if (url.equals("#") || url.equals("")) {
            return false;
        }
        
        // 确保是HTTP或HTTPS链接
        if (!url.startsWith("http://") && !url.startsWith("https://")) {
            return false;
        }
        
        return true;
    }
    
    /**
     * 判断是否为政策详情页链接
     */
    private static boolean isPolicyDetailUrl(String url) {
        if (url == null || url.isEmpty()) {
            return false;
        }
        
        // 过滤导航链接
        String[] navigationKeywords = {
            "guide", "guide", "system", "annual", "application", "gazette", "list", "link",
            "指南", "制度", "年报", "申请", "公报", "清单", "链接"
        };
        
        String lowerUrl = url.toLowerCase();
        for (String keyword : navigationKeywords) {
            if (lowerUrl.contains(keyword)) {
                return false;
            }
        }
        
        // 获取目标网站域名（河北省政府网站）
        String targetDomain = "www.hebei.gov.cn";
        String urlDomain = extractDomain(url);
        
        // 只允许爬取目标网站的链接
        if (!urlDomain.equals(targetDomain)) {
            System.out.println("⚠️ 跳过外部网站链接: " + url + " (目标域名: " + targetDomain + ", 链接域名: " + urlDomain + ")");
            return false;
        }
        
        // 只保留政策详情页链接
        // 政策详情页通常包含这些关键词
        String[] policyKeywords = {
            "detail", "content", "view", "show", "policy", "document",
            "详情", "内容", "查看", "显示", "政策", "文档"
        };
        
        for (String keyword : policyKeywords) {
            if (lowerUrl.contains(keyword)) {
                return true;
            }
        }
        
        // 如果URL包含数字ID，可能是政策详情页
        if (url.matches(".*\\d+.*")) {
            return true;
        }
        
        return true; // 默认认为是政策链接
    }
    
    /**
     * 从URL中提取域名
     */
    private static String extractDomain(String url) {
        try {
            if (url == null || url.isEmpty()) {
                return "";
            }
            
            // 移除协议前缀
            String domain = url.replaceAll("^https?://", "");
            
            // 提取域名部分（移除路径）
            int slashIndex = domain.indexOf('/');
            if (slashIndex > 0) {
                domain = domain.substring(0, slashIndex);
            }
            
            // 移除端口号
            int colonIndex = domain.indexOf(':');
            if (colonIndex > 0) {
                domain = domain.substring(0, colonIndex);
            }
            
            return domain.toLowerCase();
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 从列表页面提取文号
     */
    private static String extractDocumentNumberFromList(Element item) {
        Element zihaoElement = item.selectFirst(".xxgk_gfxwjk-list-zihao");
        if (zihaoElement != null) {
            return zihaoElement.text().trim();
        }
        
        Element altZihao = item.selectFirst(".zwgk_nzlist-zihao");
        if (altZihao != null) {
            return altZihao.text().trim();
        }
        
        return "";
    }

    /**
     * 爬取政策详情页标题和正文
     */
    public static String[] crawlPolicyDetail(String url) {
        // 配置SSL证书验证忽略
        CrawlerUtil.configureSSL();
        
        try {
            System.out.println("正在爬取详情页: " + url);
            String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";
            Document doc = Jsoup.connect(url)
                    .userAgent(userAgent)
                    .timeout(10000)
                    .get();
            
            String title = "";
            Element h2 = doc.selectFirst("h2.cont_title");
            if (h2 != null) {
                title = h2.text().trim();
            }
            
            String content = "";
            Element zoom = doc.selectFirst("#zoom");
            if (zoom != null) {
                content = zoom.html().trim();
                System.out.println("正文预览: " + content.substring(0, Math.min(200, content.length())));
            } else {
                System.out.println("未找到#zoom，正文为空！");
            }
            
            return new String[]{title, content};
        } catch (Exception e) {
            System.err.println("❌ 爬取详情页失败: " + url + " - " + e.getMessage());
            return new String[]{"", ""};
        }
    }
    
    /**
     * 从详情页面提取文号
     */
    public static String extractDocumentNumberFromDetailPage(String url) {
        // 配置SSL证书验证忽略
        CrawlerUtil.configureSSL();
        
        try {
            String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";
            Document doc = Jsoup.connect(url)
                    .userAgent(userAgent)
                    .timeout(10000)
                    .get();
            
            // 尝试多种文号选择器
            String documentNumber = "";
            
            // 方法1：尝试.bgbgs元素
            Element zihaoElement = doc.selectFirst(".bgbgs");
            if (zihaoElement != null) {
                String fullText = zihaoElement.text().trim();
                if (fullText.startsWith("发文字号：")) {
                    documentNumber = fullText.substring("发文字号：".length()).trim();
                    System.out.println("✅ 从.bgbgs获取文号: " + documentNumber);
                } else {
                    documentNumber = fullText;
                    System.out.println("✅ 从.bgbgs获取文号: " + documentNumber);
                }
            }
            
            // 方法2：尝试.xxgk_gfxwjk-list-zihao元素
            if (documentNumber.isEmpty()) {
                Element zihaoElement2 = doc.selectFirst(".xxgk_gfxwjk-list-zihao");
                if (zihaoElement2 != null) {
                    documentNumber = zihaoElement2.text().trim();
                    System.out.println("✅ 从.xxgk_gfxwjk-list-zihao获取文号: " + documentNumber);
                }
            }
            
            // 方法3：尝试.zwgk_nzlist-zihao元素
            if (documentNumber.isEmpty()) {
                Element zihaoElement3 = doc.selectFirst(".zwgk_nzlist-zihao");
                if (zihaoElement3 != null) {
                    documentNumber = zihaoElement3.text().trim();
                    System.out.println("✅ 从.zwgk_nzlist-zihao获取文号: " + documentNumber);
                }
            }
            
            // 方法4：尝试从正文中提取文号模式
            if (documentNumber.isEmpty()) {
                Element contentElement = doc.selectFirst("#zoom");
                if (contentElement != null) {
                    String content = contentElement.text();
                    // 匹配常见的文号格式
                    java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("([^\\s]+字\\[?\\d{4}\\]?\\d+号)");
                    java.util.regex.Matcher matcher = pattern.matcher(content);
                    if (matcher.find()) {
                        documentNumber = matcher.group(1);
                        System.out.println("✅ 从正文正则匹配获取文号: " + documentNumber);
                    }
                }
            }
            
            return documentNumber;
        } catch (Exception e) {
            System.err.println("❌ 提取文号失败: " + url + " - " + e.getMessage());
            return "";
        }
    }
} 